module.exports = function (app) {
  app.use('/staff/find', app.apis.getUser, app.apis.find)
  app.use('/staff/findOne', app.apis.getUser, app.apis.findOne)
  app.use('/staff/remove', app.apis.getUser, app.apis.remove)

  const oaidToId = (req, res, next) => {
    // 根据oaid 查询_id
    var oaid = req.query.oaid || req.body.oaid
    if (!oaid) return res.apiErr(31000, { param: 'oaid', message: '请求参数oaid错误或对应数据不存在'})
    app.service(req.modelName).Model.findOne({
      oaid: oaid, del: false
    }, 'oaid').then((doc) => {
      if (!doc) return res.apiErr(22000, { message: '数据不存在'})
      req.query._id = doc._id.toString()
      next()
    }).catch((error) => {
      app.apis.errorCall(error, res)
    })
  }


  app.use('/api/account/login', app.apis.getApp, (req, res) => {
    var post = req.body
    if (!post.username) return res.apiErr(31000, { param: 'username', message: '请输入用户名'})
    if (!post.password) return res.apiErr(31000, { param: 'password', message: '请输入密码'})
    app.service('staffs').Model.findOne({
      username: post.username, del: false
    }).then( doc => {
      if (!doc) return res.apiErr(32000, { param: 'username', message: '帐号不存在'})
      let user = doc.toObject()
      res.apiCb(user, {token: user._id})
    })
  })
  app.use('/api/staff/create', app.apis.getApp, app.apis.create)
  app.use('/api/staff/find', app.apis.getApp, app.apis.find)
  app.use('/api/staff/findOne', app.apis.getApp, app.apis.findOne)
  app.use('/api/staff/update', app.apis.getApp, oaidToId, app.apis.update)
  app.use('/api/staff/update_passwd', app.apis.getApp, oaidToId, app.apis.update)
}
